/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.cux.flexfield.controller;

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.redragon.framework.springmvc.ControllerSupport;
import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.JsonDataUtil;
import org.redragon.framework.util.JsonUtil;

import com.erp.cux.flexfield.dao.data.DataBox;
import com.erp.cux.flexfield.dao.model.CuxFlexFieldLine;
import com.erp.cux.flexfield.dao.model.CuxFlexFieldLineCO;
import com.erp.cux.flexfield.service.CuxFlexFieldLineService;
import com.erp.dataset.dao.model.SysDatasetType;
import com.erp.dataset.service.DatasetCommonService;
import com.erp.dataset.service.SysDatasetTypeService;

@Controller
@RequestMapping("/web/cuxFlexFieldLine")
public class CuxFlexFieldLineWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(CuxFlexFieldLineWebController.class);
    
    //服务层注入
    @Autowired
    private CuxFlexFieldLineService cuxFlexFieldLineService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private SysDatasetTypeService sysDatasetTypeService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getCuxFlexFieldLineList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2021-02-23 18:07:20
     * @author 
     * @param pages
     * @param cuxFlexFieldLineCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getCuxFlexFieldLineList")
    public String getCuxFlexFieldLineList(Pages pages, CuxFlexFieldLineCO cuxFlexFieldLineCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        pages.setMax(100);
        
        //分页查询数据
        List<CuxFlexFieldLine> cuxFlexFieldLineList = this.cuxFlexFieldLineService.getCuxFlexFieldLineListByHeadCode(pages, cuxFlexFieldLineCO);
        
        //字段类型
        Map fieldAttributeTypeMap = DataBox.getFieldAttributeTypeMap();
        
        //页面属性设置
        model.addAttribute("cuxFlexFieldLineList", cuxFlexFieldLineList);
        model.addAttribute("pages", pages);
        model.addAttribute("fieldAttributeTypeMap", fieldAttributeTypeMap);
        
        return "cuxFlexField/tab/lineTab";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2021-02-23 18:07:20
     * @author 
     * @param cuxFlexFieldLine
     * @param model
     * @return String
     *
     */
    @RequestMapping("getCuxFlexFieldLine")
    public String getCuxFlexFieldLine(CuxFlexFieldLine cuxFlexFieldLine, Model model) {
        //查询要编辑的数据
        if(cuxFlexFieldLine!=null&&cuxFlexFieldLine.getFfLineId()!=null) {
            cuxFlexFieldLine = this.cuxFlexFieldLineService.getDataObject(cuxFlexFieldLine.getFfLineId());
        }
        
        
        //字段类型
        Map fieldAttributeTypeMap = DataBox.getFieldAttributeTypeMap();
        //获取所有值集
        List<SysDatasetType> sysDatasetTypeList = this.sysDatasetTypeService.getDataObjects();
        
        //页面属性设置
        model.addAttribute("cuxFlexFieldLine", cuxFlexFieldLine);
        model.addAttribute("fieldAttributeTypeMap", fieldAttributeTypeMap);
        model.addAttribute("sysDatasetTypeList", sysDatasetTypeList);
        
        return "cuxFlexField/pop/addLineModal";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2021-02-23 18:07:20
     * @author 
     * @param cuxFlexFieldLine
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editCuxFlexFieldLine")
    @ResponseBody
    public String editCuxFlexFieldLine(@Valid CuxFlexFieldLine cuxFlexFieldLine, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        try {
            //对当前编辑的对象初始化默认的字段
            if(cuxFlexFieldLine.getFfLineId()==null) {
                cuxFlexFieldLine.setFfLineCode(SnowFlake.getConcurrentId().toString());
            }
            
            //保存编辑的数据
            this.cuxFlexFieldLineService.insertOrUpdateDataObject(cuxFlexFieldLine);
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2021-02-23 18:07:20
     * @author 
     * @param cuxFlexFieldLine
     * @return String
     *
     */
    @RequestMapping("deleteCuxFlexFieldLine")
    @ResponseBody
    public String deleteCuxFlexFieldLine(CuxFlexFieldLine cuxFlexFieldLine, RedirectAttributes attr) {
        try {
            //删除数据前验证数据
            if(cuxFlexFieldLine!=null&&cuxFlexFieldLine.getFfLineId()!=null) {
                //删除数据
                this.cuxFlexFieldLineService.deleteDataObject(cuxFlexFieldLine);
            }
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
}
